﻿'''
Created on May 12, 2011

@author: Jialai_Zhu
'''
#!/usr/bin/python 
# -*- coding: gb2312 -*- 
# 
from db import *
#DB_QUERY_DAILY=None,DB_QUERY_MAIN=None,DB_QUERY_KEYWORD=None


def default_add_zq_daily(domain,keyword,time,title='',text='',option='',error='200',type='',url='',check_domain=True):
    global DB_QUERY_DAILY
    if check_domain==True:
        if find_zq_daily(domain)==True:
            return False
    key={}
    key['domain']=domain
    key['keyword']=keyword
    key['time']=time
    key['title']=title
    key['text']=text
    key['option']=option
    key['error']=error
    try:
        key['detected_date']=''
        from db_zq import extract_fake_zq
        ZQ_TYPE=extract_fake_zq()
        key['detected_date']=ZQ_TYPE.findate(text)
    except:        pass
    
    key['url']=url
    item=DB_QUERY_DAILY.find({'keyword':keyword,'domain':''},1)
        
    if item==None:
        DB_QUERY_DAILY.insert(key)
    else:
        DB_QUERY_DAILY.update_item(item, key)
    return True                
add_zq_daily=default_add_zq_daily
DB_QUERY_DAILY=None
DB_QUERY_KEYWORD=None
def db_init(name):
    global DB_QUERY_DAILY,DB_QUERY_MAIN,DB_QUERY_KEYWORD
    if DB_QUERY_DAILY==None:
        DB_QUERY_DAILY=mongo_query_base(db_name=name,table_name='main')
    if DB_QUERY_KEYWORD==None:
        DB_QUERY_KEYWORD=mongo_query_base(db_name=name,table_name='keyword')
    if name=="zq":
        import db_zq
        add_zq_daily=db_zq.add_zq_daily

class gb2312file():
    def __init__(self,name,mode='wb'):
        self.f=open(name,mode)
    def write(self,data):
        if isinstance(data,unicode)==True:
            d=data.encode('gb2312')
        else:
            d=data
        self.f.write(d)
    def writeln(self,data):
        self.write(data)
        self.write('\r\n')

#ZQ_TYPE=extract_fake_zq()
def is_key_searched(key):
    global DB_QUERY_KEYWORD
    item=DB_QUERY_KEYWORD.find(key)
    return False
def set_key_searched(key,result=0,sucessed=True):
    global DB_QUERY_KEYWORD
    item=DB_QUERY_KEYWORD.find({'key':key},1)
    import datetime
    today=datetime.date.today()
    date=today.strftime("%Y%m%d")
    if item==None:
        DB_QUERY_KEYWORD.insert({'key':key,'date':date,'result':result,'today':result,'ok':sucessed,'status':'done'})
    else:
        total=int(item['result'])
        DB_QUERY_KEYWORD.update_item(item, {'date':date,'result':total,'today':result,'ok':sucessed,'status':'done'})  
def query_keyword_status(key):
    global DB_QUERY_KEYWORD
    item=DB_QUERY_KEYWORD.find({'key':key},1)
    import datetime
    today=datetime.date.today()
    date=unicode(today.strftime("%Y%m%d"))
    if item==None:
        return False
    if date!=item['date']:
        DB_QUERY_KEYWORD.update_item(item,{'status':'process'})
        return False
    else:
        try:
            if item['status']=='process':
                return True
        except:
            pass
        try:
            return item['ok']==True
        except:
            return False

def find_zq_daily(domain):
    global DB_QUERY_DAILY
    if domain=='':
        return False
    return DB_QUERY_DAILY.has({'domain':domain})
def find_zq_key(key):
    global DB_QUERY_DAILY
    i=DB_QUERY_DAILY.find({'keyword':key},1)  
    if i==None:
        return False         
    if i['domain']=='':
        return False
    else:
        return True

#def update_detected_date():
#    zq_type=extract_fake_zq()
#    for item in DB_QUERY_DAILY.find({}, 0):
#        try:
#            title=item['text']
#            ret=zq_type.findate(title)
#            if ret==None:
#                continue            
#            DB_QUERY_DAILY.update_item(item, {'detected_date':ret})
#        except Exception,ex:
#            pass
def update_key_record():
    for item in DB_QUERY_DAILY.find({}, 0):
        try:
            key=item['keyword']
            set_key_searched(key)
        except Exception,ex:
            pass    


 
if __name__ == '__main__':
    s=u'&#x6D77;&#x76CA;&#x90A6;&#x6295;   &#x53F8;&#x6D77;&#x76CA;&#x90A6;&#x6295;'
    pass;
#main_companyname()
    
